//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TridiagonalOperator.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (Cephei::QL::Math::IArray^ low, Cephei::QL::Math::IArray^ mid, Cephei::QL::Math::IArray^ high) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CArray^ _Clow;
    CArray^ _Cmid;
    CArray^ _Chigh;
    try
    {
#ifdef HANDLE
        _phTridiagonalOperator = NULL;
#endif
        _Clow = safe_cast<CArray^> (low);
        _Clow->Lock();
        QuantLib::Array& _low = static_cast<QuantLib::Array&> (_Clow->GetReference ()); 
        _Cmid = safe_cast<CArray^> (mid);
        _Cmid->Lock();
        QuantLib::Array& _mid = static_cast<QuantLib::Array&> (_Cmid->GetReference ()); 
        _Chigh = safe_cast<CArray^> (high);
        _Chigh->Lock();
        QuantLib::Array& _high = static_cast<QuantLib::Array&> (_Chigh->GetReference ()); 
        _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (new QuantLib::TridiagonalOperator ( _low,  _mid,  _high ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Clow != nullptr) _Clow->Unlock();
        if (_Cmid != nullptr) _Cmid->Unlock();
        if (_Chigh != nullptr) _Chigh->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (Microsoft::FSharp::Core::FSharpOption<UInt64>^ size) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phTridiagonalOperator = NULL;
#endif
        QuantLib::Size _size = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (size) ? (QuantLib::Size)ValueHelper::Convert (size->Value) : 0); //4
        _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (new QuantLib::TridiagonalOperator ( _size ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (boost::shared_ptr<QuantLib::TridiagonalOperator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTridiagonalOperator = NULL;
#endif
	_ppTridiagonalOperator = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (QuantLib::TridiagonalOperator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTridiagonalOperator = NULL;
#endif
	_ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (&childNative);
    
    _TridiagonalOperatorOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (CTridiagonalOperator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTridiagonalOperator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTridiagonalOperator = NULL;
#endif
	if (!t->IsSubclassOf(CTridiagonalOperator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (QuantLib::Handle<QuantLib::TridiagonalOperator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTridiagonalOperator = &childNative;
	_ppTridiagonalOperator = &static_cast<boost::shared_ptr<QuantLib::TridiagonalOperator>>(childNative.currentLink());
    
    _TridiagonalOperatorOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (QuantLib::Handle<QuantLib::TridiagonalOperator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTridiagonalOperator = &childNative;
	_ppTridiagonalOperator = &static_cast<boost::shared_ptr<QuantLib::TridiagonalOperator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::CTridiagonalOperator (QuantLib::TridiagonalOperator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTridiagonalOperator = NULL;
#endif
	_ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (new QuantLib::TridiagonalOperator (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::~CTridiagonalOperator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTridiagonalOperator != NULL)
    {
	    delete _ppTridiagonalOperator;
        _ppTridiagonalOperator = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::!CTridiagonalOperator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTridiagonalOperator != NULL)
    {
	    delete _ppTridiagonalOperator;
    }
}
QuantLib::TridiagonalOperator& Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::GetReference ()
{
    if (_ppTridiagonalOperator == NULL) throw REFNEW NativeNullException ();
	return **_ppTridiagonalOperator;
}
boost::shared_ptr<QuantLib::TridiagonalOperator>& Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::GetShared ()
{
    if (_ppTridiagonalOperator == NULL) throw REFNEW NativeNullException ();
	return *_ppTridiagonalOperator;
}
QuantLib::TridiagonalOperator* Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::GetPointer ()
{
    if (_ppTridiagonalOperator == NULL) throw REFNEW NativeNullException ();
	return &**_ppTridiagonalOperator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TridiagonalOperator>& Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::GetHandle ()
{
	if (_phTridiagonalOperator == NULL)
	{
		_phTridiagonalOperator = new Handle<QuantLib::TridiagonalOperator> (*_ppTridiagonalOperator);
	}
	return *_phTridiagonalOperator;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::HasNative () 
{
	return (_ppTridiagonalOperator != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::Diagonal::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppTridiagonalOperator)->diagonal ( );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::IsTimeDependent::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppTridiagonalOperator)->isTimeDependent ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::LowerDiagonal::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppTridiagonalOperator)->lowerDiagonal ( );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::SetFirstRow (Double prm1, Double prm2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _prm1 = (QuantLib::Real)ValueHelper::Convert (prm1); //a
        QuantLib::Real _prm2 = (QuantLib::Real)ValueHelper::Convert (prm2); //a
    	(*_ppTridiagonalOperator)->setFirstRow ( _prm1,  _prm2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::SetLastRow (Double prm1, Double prm2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _prm1 = (QuantLib::Real)ValueHelper::Convert (prm1); //a
        QuantLib::Real _prm2 = (QuantLib::Real)ValueHelper::Convert (prm2); //a
    	(*_ppTridiagonalOperator)->setLastRow ( _prm1,  _prm2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::SetMidRow (UInt64 prm1, Double prm2, Double prm3, Double prm4)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _prm1 = (QuantLib::Size)ValueHelper::Convert (prm1); //a
        QuantLib::Real _prm2 = (QuantLib::Real)ValueHelper::Convert (prm2); //a
        QuantLib::Real _prm3 = (QuantLib::Real)ValueHelper::Convert (prm3); //a
        QuantLib::Real _prm4 = (QuantLib::Real)ValueHelper::Convert (prm4); //a
    	(*_ppTridiagonalOperator)->setMidRow ( _prm1,  _prm2,  _prm3,  _prm4 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::SetMidRows (Double prm1, Double prm2, Double prm3)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _prm1 = (QuantLib::Real)ValueHelper::Convert (prm1); //a
        QuantLib::Real _prm2 = (QuantLib::Real)ValueHelper::Convert (prm2); //a
        QuantLib::Real _prm3 = (QuantLib::Real)ValueHelper::Convert (prm3); //a
    	(*_ppTridiagonalOperator)->setMidRows ( _prm1,  _prm2,  _prm3 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::SetTime (Double t)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	(*_ppTridiagonalOperator)->setTime ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTridiagonalOperator)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::Swap (Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ prm1)
{
    CTridiagonalOperator^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CTridiagonalOperator^> (prm1);
        _Cprm1->Lock();
        QuantLib::TridiagonalOperator& _prm1 = static_cast<QuantLib::TridiagonalOperator&> (_Cprm1->GetReference ()); 
    	(*_ppTridiagonalOperator)->swap ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::UpperDiagonal::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppTridiagonalOperator)->upperDiagonal ( );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator::SolveFor (Cephei::QL::Math::IArray^ rhs, Cephei::QL::Math::IArray^ result)
{
    CArray^ _Crhs;
    CArray^ _Cresult;
    try
    {
        _Crhs = safe_cast<CArray^> (rhs);
        _Crhs->Lock();
        QuantLib::Array& _rhs = static_cast<QuantLib::Array&> (_Crhs->GetReference ()); 
        _Cresult = safe_cast<CArray^> (result);
        _Cresult->Lock();
        QuantLib::Array& _result = static_cast<QuantLib::Array&> (_Cresult->GetReference ()); 
    	(*_ppTridiagonalOperator)->solveFor ( _rhs,  _result );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crhs != nullptr) _Crhs->Unlock();
        if (_Cresult != nullptr) _Cresult->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator_Factory::Create (Cephei::QL::Math::IArray^ low, Cephei::QL::Math::IArray^ mid, Cephei::QL::Math::IArray^ high)
{
    return REFNEW CTridiagonalOperator ( low,  mid,  high);
}
Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ Cephei::QL::Methods::Finitedifferences::CTridiagonalOperator_Factory::Create (Microsoft::FSharp::Core::FSharpOption<UInt64>^ size)
{
    return REFNEW CTridiagonalOperator ( size);
}
